﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using CMS.Helper;
using RBAC;
using RBAC.ORM;

namespace RBAC.APP
{
    public class Department
    {
        #region Fields
        /// <summary>
        /// 栏目Id
        /// </summary>
        private string id;
        /// <summary>
        /// 栏目Id
        /// </summary>
        public string Id
        {
            get { return id; }
            //set { id = value; }
        }
        /// <summary>
        /// 栏目名称
        /// </summary>
        private string name;
        /// <summary>
        /// 栏目名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 栏目描述
        /// </summary>
        private string description;
        /// <summary>
        /// 栏目描述
        /// </summary>
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        /// <summary>
        /// 父栏目
        /// </summary>
        private string parent;
        /// <summary>
        /// 父栏目
        /// </summary>
        public Department Parent
        {
            get { return ReadDepartment(this.parent); }
            //set { parent = value.id; }
        }
        /// <summary>
        /// 顺序
        /// </summary>
        private int sequence;
        /// <summary>
        /// 顺序
        /// </summary>
        public int Sequence
        {
            get { return sequence; }
            //set { sequence = value; }
        }
        /// <summary>
        /// 可见性
        /// </summary>
        private DepartmentVisiable visiable;
        /// <summary>
        /// 可见性
        /// </summary>
        public DepartmentVisiable Visiable
        {
            get { return visiable; }
            //set { visiable = value; }
        }
        #endregion Fields

        #region Functions
        /// <summary>
        /// 同级兄弟
        /// </summary>
        public List<Department> Brother
        {
            get
            {
                //--------------------------------------------------------
                //返回与此Department具有相同uppart相同的Department对象
                //注意：返回的Department对象顺序按照sequence升序。并且不包含当前的Department。
                //结果以泛型返回。
                //调用单个Department对象可以使用ReadDepartment方法。
                //--------------------------------------------------------
                List<Department> myBrothers = new List<Department>();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
                myOpt = optSDepartment.GetModel(this.id);
                DataSet ds = optSDepartment.GetList("uppart = " + myOpt.uppart);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        myBrothers.Add(ReadDepartment(ds.Tables[0].Rows[i]["id"].ToString()));
                    }
                    //寻找当前Department并删除
                    for (int j = 0; j < myBrothers.Count; j++)
                    {
                        if (myBrothers[j].id == this.id)
                        {
                            myBrothers.Remove(myBrothers[j]);
                        }
                    }
                    return myBrothers;
                }
            }
        }
        /// <summary>
        /// 子栏目
        /// </summary>
        public List<Department> Child
        {
            //--------------------------------------------------------
            //返回uppart为此Department.id的Department集合。
            //注意：返回的Department对象顺序按照sequence升序。结果以泛型返回。
            //调用单个Department对象可以使用ReadDepartment方法。
            //--------------------------------------------------------
            get 
            {
                List<Department> myChilds = new List<Department>();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                DataSet ds = optSDepartment.GetList("uppart = " + this.id);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
                    {
                        myChilds.Add(ReadDepartment(ds.Tables[0].Rows[i]["id"].ToString()));
                    }
                    return myChilds;
                }
            }
        }
        
        /// <summary>
        /// 添加子栏目
        /// </summary>
        /// <param name="childId">子栏目Id</param>
        /// <param name="childName">子栏目名称</param>
        /// <param name="childDescription">子栏目描述</param>
        /// <param name="childVisiable">子栏目可见性</param>
        public int AddChild(string childId, string childName, string childDescription, DepartmentVisiable childVisiable)
        {
            //*****10101*****
            //调用InsertChild方法
            //注意：1.childSequence为当前栏目的子栏目数量＋1
            //注意：2.报错的代码均使用InsertChild方法中的错误代码
            //--------------------------------------------------------
            if (string.IsNullOrEmpty(childId) || !CommonFunctions.IsEnglish(childId) || childId.Length > 50)
            {
                return 101022;
            }
            else
            {
                childId = childId.ToLower();
            }

            if (string.IsNullOrEmpty(childName) || childName.Length > 50)
            {
                return 101023;
            }

            if (childVisiable == null)
            {
                return 101024;
            }

            int childSequence = this.Child.Count + 1;
            return InsertChild(childId, childName, childDescription, childVisiable, childSequence);

        }

        /// <summary>
        /// 插入子栏目**
        /// </summary>
        /// <param name="childId">子栏目Id</param>
        /// <param name="childName">子栏目名称</param>
        /// <param name="childDescription">子栏目描述</param>
        /// <param name="childVisiable">子栏目可见性</param>
        /// <param name="childSequence">栏目位置</param>
        public int InsertChild(string childId, string childName, string childDescription, DepartmentVisiable childVisiable, int childSequence)
        {
            //*****10102*****
            //向表：SDepartment中添加记录
            //（1）参数childId：不能为空，不能长于50，全部为英文，全转换为小写。报错101022.
            //（2）参数childName：不能为空，不能长于50，中英文皆可。报错101023.
            //（3）参数childVisiable：不能为空。报错101024
            //（4）参数childDescription：可为空。
            //（5）参数uppart：当前Department的id
            //（6）参数childsequence：相同uppart相同sequence的栏目存在否？
            //         （a）若存在，则从此sequence开始之后的所有栏目sequence＋1并保存（请不要调用其它类内方法，以免留下log）
            //         （b）若不存在，则childSequence调整为兄弟个数＋1
            //         （c）6阶段发生的任何错误，报错101025
            //（7）参数ifRoot：0（不是根目录）；ifLeaf：1（是树叶）。
            //（8）为本栏目增加子栏目后，考察本栏目的Root或Leaf情况。若本栏目原为Leaf，则要将此项置为0.此项报错101026.
            //（9）返回：int。成功返回101021，失败返回101029.
            //--------------------------------------------------------
            //需要Log的情况：101021，101029，101025，101026.

            RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
            RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();

            if (string.IsNullOrEmpty(childId) || !CommonFunctions.IsEnglish(childId) || childId.Length > 50)
            {
                return 101022;
            }
            else
            {
                childId = childId.ToLower();
            }

            if (string.IsNullOrEmpty(childName) || childName.Length > 50)
            {
                return 101023;
            }

            if (childVisiable == null)
            {
                return 101024;
            }

            string uppart = this.id;
            int childsequence;
            
            //optSDepartment.GetModel(
            
        }

        /// <summary>
        /// 更新栏目*
        /// </summary>
        public int Update()
        {
            //*****10103*****
            //更新表：SDepartment中的记录
            //更新字段：name，description。
            //（1）Name不能为空，若为空，报错101032.
            //（2）更新成功，返回101031；更新失败，返回101039.
            //--------------------------------------------------------
            //需要Log的情况：101031，101039
            if (string.IsNullOrEmpty(this.Name))
            {
                return 101032;
            }
            try
            {
                RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                
                myOpt.id = this.id;
                myOpt.name = this.name;
                myOpt.description = this.description;
                myOpt.sequence = this.sequence;
                if (this.visiable == DepartmentVisiable.Visiable)
                {
                    myOpt.visiable = 1;
                }
                else
                {
                    myOpt.visiable = 0;                    
                }
                
                myOpt.ifLeaf = 0; //**
                myOpt.ifRoot = 0; //**
                myOpt.uppart = null; //**

                bool OK = optSDepartment.Update(myOpt);
                if (!OK)
                {
                    throw new Exception("optSDepartment.Update(myOpt)错误;" + this.id);
                }

                Operator.MakeLog(ObjectType.DEPT, "101031 " + this.id);
                return 101031;
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101039 " + this.id);
                return 101039;
            }
        }

        /// <summary>
        /// 删除子栏目
        /// </summary>
        /// <param name="childId">子栏目Id</param>
        public int DeleteChild(string childId)
        {
            //*****10104*****
            //删除一个子栏目，删除表：SDepartment中的一条记录
            //（1）参数childID不能为空，否则报错101042.
            //（2）若此栏目不存在（可使用ReadDepartment判断），报错101043
            //（3）若子栏目不为叶子（有Child）则不可删除，报错101044.
            //（4）删除表：SURD中含有此栏目（departID）的所有记录。过程中的错误记录为101045.
            //（5）删除此子栏目
            //删除成功，返回101041；操作失败，返回101049.
            //--------------------------------------------------------
            //需要Log的情况：101041，101049，101045

            RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
            RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
            RBAC.ORM.DAL.SURD optSURD = new RBAC.ORM.DAL.SURD();
            myOpt = optSDepartment.GetModel(childId);

            if (string.IsNullOrEmpty(childId))
            {
                return 101042;
            }

            if (ReadDepartment(childId) == null)
            {
                return 101043;
            }

            if (myOpt.ifLeaf != 1)
            {
                return 101044;
            }

            try
            {
                DataSet myDs = optSURD.GetList("departID = " + childId);
                decimal[] delId = new decimal[] { };
                for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
                {
                    delId[i] = Convert.ToDecimal(myDs.Tables[0].Rows[i]["id"]);
                }
                foreach (int j in delId)
                {
                    optSURD.Delete(delId[j]);
                }
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101045 " + childId);
                return 101045;
            }

            try
            {
                bool OK = optSDepartment.Delete(childId);
                if (!OK)
                {
                    throw new Exception("optSDepartment.Delete(childId)失败");
                }
                else
                {
                    Operator.MakeLog(ObjectType.DEPT, "101041 " + childId);
                    return 101041;
                }
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101049 " + childId);
                return 101049;
            }
            
        }

        /// <summary>
        /// 删除子栏目
        /// </summary>
        /// <param name="childDepartment">子栏目对象</param>
        public int DeleteChild(Department childDepartment)
        {
            //*****10105*****
            //此方法调用DeleteChild方法。
            //--------------------------------------------------------
            string childId = childDepartment.id;
            return DeleteChild(childId);
        }

        /// <summary>
        /// 移动栏目至*
        /// </summary>
        /// <param name="destinationId">目标栏目下</param>
        public int MoveUnder(string destinationId)
        {
            //*****10106*****
            //（1）参数：destinationId不能为空，报错101062
            //（2）destinationId栏目是否存在（调用ReadDepartment方法），若不存在，报错101063
            //（3）destinationId栏目必须为叶子节点（ifLeaf==1），若不是叶子节点，报错101064
            //（4）更新表：SDepartment中此记录uppart为destinationId。
            //移动成功，返回101061；操作失败，返回101069
            //--------------------------------------------------------
            //需要Log的情况：101061，101069

            RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
            RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
            
            if (string.IsNullOrEmpty(destinationId))
            {
                return 101062;
            }
            if (ReadDepartment(destinationId) == null)
            {
                return 101063;
            }
            
            myOpt = optSDepartment.GetModel(destinationId);
            if (!(myOpt.ifLeaf == 1))
            {
                return 101064;
            }

            try
            {
                myOpt = optSDepartment.GetModel(this.id); //**
                myOpt.uppart = destinationId;
                optSDepartment.Update(myOpt);
                Operator.MakeLog(ObjectType.DEPT, "101061 " + this.id);
                return 101061;
            }
            catch
            {
                Operator.MakeLog(ObjectType.DEPT, "101069 " + this.id);
                return 101069;
            }

            
        }

        /// <summary>
        /// 移动栏目至
        /// </summary>
        /// <param name="destinationDepartment">目标栏目下</param>
        public int MoveUnder(Department destinationDepartment)
        {
            //*****10107*****
            //调用方法MoverUnder
            //-------------------------------------------------------
            return MoveUnder(destinationDepartment.id);
        }

        /// <summary>
        /// 移动栏目至*
        /// </summary>
        /// <param name="departmentId">目标栏目后</param>
        public int MoveAfter(string destinationId)
        {
            //*****10108*****
            //（1）参数：destinationId不能为空，报错101082
            //（2）destinationId栏目是否存在（调用ReadDepartment方法），若不存在，报错101083
            //（3）检测destinationId是否有兄弟栏目，若其sequece后还有其它栏目，则其后栏目的sequence各向后移动1。出现错误，返回101084
            //（4）移动栏目，修改此栏目的uppart、sequence
            //移动成功，返回101081；操作失败，返回101089
            //--------------------------------------------------------
            //需要Log的情况：101081，101089，101084

            RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
            RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();

            if (string.IsNullOrEmpty(destinationId))
            {
                return 101082;
            }
            if (ReadDepartment(destinationId) == null)
            {
                return 101083;
            }

            Department destDept = ReadDepartment(destinationId);
            List<Department> destBrothers = destDept.Brother;
            if (destBrothers != null)
            {
                try
                {
                    for (int i = 0; i < destBrothers.Count; i++)
                    {
                        if (destBrothers[i].sequence > destDept.sequence) //在destinationId的Brothers中找sequence在其后的Brothers
                        {
                            destBrothers[i].sequence++; //找到的话就让其后的Brother的sequence++
                            myOpt = optSDepartment.GetModel(destBrothers[i].id); //保存被++的Brothers
                            optSDepartment.Update(myOpt);
                        }
                    }
                }
                catch
                {
                    Operator.MakeLog(ObjectType.DEPT, "101084" + this.id);
                    return 101084;
                }
            }

            try
            {
                myOpt.id = this.id;
                myOpt.name = this.name;
                myOpt.description = this.description;
                myOpt.uppart = optSDepartment.GetModel(destinationId).uppart; //*
                myOpt.sequence = optSDepartment.GetModel(destinationId).sequence + 1; //*
                //myOpt.sequence = ReadDepartment(destinationId).sequence + 1;

                bool OK = optSDepartment.Update(myOpt);
                if (!OK)
                {
                    throw new Exception("optSDepartment.Update(myOpt)失败," + this.id);
                }
                Operator.MakeLog(ObjectType.DEPT, "101081" + this.id);
                return 101081;
            }
            catch
            {
                Operator.MakeLog(ObjectType.DEPT, "101089" + this.id);
                return 101089;
            }
            
        }

        /// <summary>
        /// 移动栏目至
        /// </summary>
        /// <param name="departmentObject">目标栏目后</param>
        public int MoveAfter(Department departmentObject)
        {
            //*****10109*****
            //调用MoverAfter方法
            //--------------------------------------------------------
            return MoveAfter(departmentObject.id);
        }
        #endregion Functions

        #region StaticFunctions
        /// <summary>
        /// 激活栏目
        /// </summary>
        /// <param name="departmentId">栏目Id</param>
        public static int DepartmentActivate(string departmentId)
        {
            //*****10110*****
            //激活时将visiable调整为1
            //成功返回，101101；失败返回，101109.
            //--------------------------------------------------------
            //需要Log的情况：101101，101109
            try
            {
                RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                myOpt = optSDepartment.GetModel(departmentId);
                myOpt.visiable = 1;
                optSDepartment.Update(myOpt);
                Operator.MakeLog(ObjectType.DEPT, "101101" + departmentId);
                return 101101;
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101109 " + departmentId);
                return 101109;
            }
        }

        /// <summary>
        /// 激活栏目
        /// </summary>
        /// <param name="departmentObject">栏目对象</param>
        public static int DepartmentActivate(Department departmentObject)
        {
            //*****10111*****
            //调用DepartmentActive方法
            //--------------------------------------------------------
            return DepartmentActivate(departmentObject.Id);
        }

        /// <summary>
        /// 停用栏目
        /// </summary>
        /// <param name="departmentId">栏目Id</param>
        public static int DepartmentDeactivate(string departmentId)
        {
            //*****10112*****
            //停用时将visiable调整为0
            //成功返回，101121；失败返回，101129.
            //--------------------------------------------------------
            //需要Log的情况：101121，101129
            try
            {
                RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                myOpt = optSDepartment.GetModel(departmentId);
                myOpt.visiable = 0;
                optSDepartment.Update(myOpt);
                Operator.MakeLog(ObjectType.DEPT, "101121" + departmentId);                
                return 101121;
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101129 " + departmentId);                
                return 101129;
            }
        }

        /// <summary>
        /// 停用栏目
        /// </summary>
        /// <param name="departmentObject">栏目对象</param>
        public static int DepartmentDeactivate(Department departmentObject)
        {
            //*****10113*****
            //调用DepartmentDeactivate方法
            //-------------------------------------------------------
            return DepartmentDeactivate(departmentObject.Id);
        }

        /// <summary>
        /// 阅读栏目
        /// </summary>
        /// <param name="departmentId">栏目Id</param>
        public static Department ReadDepartment(string departmentId)
        {
            //根据DepartmentId获得Department对象
            //若参数：departmentId为空，或对象不存在，返回null
            if (!string.IsNullOrEmpty(departmentId))
            {
                Department myDept = new Department();
                RBAC.ORM.Model.SDepartment myOpt = new RBAC.ORM.Model.SDepartment();
                RBAC.ORM.DAL.SDepartment optSDepartment = new RBAC.ORM.DAL.SDepartment();
                myOpt = optSDepartment.GetModel(departmentId);
                if (myOpt == null) //我看了dataOpt.GetModel()，失败返回null。所以我想这里应该可以不用try了。直接判断null。
                {
                    return null;
                }
                myDept.id = myOpt.id;
                myDept.name = myOpt.name;
                if (myOpt.visiable == 1)
                {
                    myDept.visiable = DepartmentVisiable.Visiable;
                }
                else
                {
                    myDept.visiable = DepartmentVisiable.Unvisiable;                    
                }
                myDept.sequence = (int)myOpt.sequence;
                myDept.description = myOpt.description;
                return myDept;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 搜索栏目
        /// </summary>
        /// <param name="nameOrDescription">栏目名或栏目描述</param>
        public static List<Department> SearchDepartments(string nameOrDescription, int? howMany)
        {
            //栏目名称或描述中包含参数的栏目，若为空则搜索所有栏目
            //结果为泛型，按照栏目Id升序
            //参数howMany若为空，则调用RBACAPPDefaultValue.HowMany_DepartmentToRead，若仍失败，默认为100
            List<Department> myDeptList = new List<Department>();
            if (howMany == null)
            {
                try
                {
                    howMany = RBACAPPDefaultValue.HowMany_DepartmentToRead;
                }
                catch (Exception)
                {
                    howMany = 100;
                }
            }

            ORM.DAL.SDepartment optSDepartment = new ORM.DAL.SDepartment();
            DataSet ds = new DataSet();
            if (!string.IsNullOrEmpty(nameOrDescription))
            {
                ds = optSDepartment.GetList("where name like '%" + nameOrDescription + "%' or discription like '%" + nameOrDescription + "%' ORDER BY Id ASC");
            }
            else
            {
                ds = optSDepartment.GetList("1=1 ORDER BY Id ASC");
            }
            if (ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                for (int i = 0; i <= howMany; i++)
                {
                    myDeptList.Add(ReadDepartment(ds.Tables[0].Rows[i]["Id"].ToString()));
                }
                return myDeptList;
            }
        }

        /// <summary>
        /// 添加根目录
        /// </summary>
        /// <param name="rootDeptId">根目录Id</param>
        /// <param name="rootDeptName">根目录名称</param>
        /// <param name="rootDeptDescription">根目录描述</param>
        public static int AddRootDepartment(string rootDeptId, string rootDeptName, string rootDeptDescription, DepartmentVisiable rootDeptVisiable)
        {
            //*****10113*****
            //向表：SDepartment添加记录
            //（1）参数rootDeptId：不能为空，必须全英文，（报错：101132）。全部转换为小写。
            //          若此栏目Id已经存在，报错101133。
            //（2）参数rootDeptName：不能为空。报错101134
            //（3）参数rootDeptDescription：可以为空
            //（4）参数rootDeptVisiable：不能为空。报错101135
            //（5）uppart为空字符串
            //（6）sequence为根目录的数量＋1
            //（7）ifRoot：1（根目录：1）；ifLeaf：1（叶子：1）
            //（8）返回：int
            //          添加成功，返回101131；添加失败，返回101139.
            //-------------------------------------------------------
            //需要Log的情况：101131，101139
            ORM.Model.SDepartment myOpt = new ORM.Model.SDepartment();
            ORM.DAL.SDepartment optSDepartment = new ORM.DAL.SDepartment();
            //参数处理
            if (string.IsNullOrEmpty(rootDeptId) && !CommonFunctions.IsEnglish(rootDeptId))
            {
                return 101132;
            }
            rootDeptId = rootDeptId.ToLower();
            if (optSDepartment.Exists(rootDeptId))
            {
                return 101133;
            }
            if (string.IsNullOrEmpty(rootDeptName))
            {
                return 101134;
            }
            if (rootDeptVisiable == null)
            {
                return 101135;
            }
            string uppart = "";
            DataSet myDs = optSDepartment.GetList("ifRoot = 1");
            int sequence = myDs.Tables[0].Rows.Count;
            //写数据
            try
            {
                myOpt.id = rootDeptId;
                myOpt.name = rootDeptName;
                myOpt.description = rootDeptDescription;
                if (rootDeptVisiable == DepartmentVisiable.Visiable)
                {
                    myOpt.visiable = 1;
                }
                if (rootDeptVisiable == DepartmentVisiable.Unvisiable)
                {
                    myOpt.visiable = 0;
                }
                myOpt.ifRoot = 1;
                myOpt.ifLeaf = 1;
                myOpt.uppart = uppart;
                myOpt.sequence = sequence + 1;
                bool OK = optSDepartment.Add(myOpt);
                if (OK)
                {
                    Operator.MakeLog(ObjectType.DEPT, "101131 " + rootDeptId);
                    return 101131;
                }
                else
                { 
                    throw new Exception("dataOpt.Add()失败");
                }
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101139 " + rootDeptId);                
                return 101139;
            }
        }

        /// <summary>
        /// 删除栏目
        /// </summary>
        /// <param name="rootDepartmentId">根栏目Id</param>
        public static int DeleteRootDepartment(string rootDepartmentId)
        {
            //*****10114*****
            //删除一个根栏目，删除表：SDepartment中的一条记录
            //（1）参数rootDepartmentId不能为空，否则报错101142.
            //（2）若此栏目不存在（可使用ReadDepartment判断），报错101143
            //（3）若此栏目不为叶子（有Child）则不可删除，报错101144.
            //（4）删除表：SURD中含有此栏目（rootDepartmentID）的所有记录。过程中的错误记录为101145.
            //（5）删除此栏目
            //删除成功，返回101141；操作失败，返回101149.
            //--------------------------------------------------------
            //需要Log的情况：101141，101149，101145
            ORM.Model.SDepartment modelOpt = new ORM.Model.SDepartment();
            ORM.DAL.SDepartment dataOpt = new ORM.DAL.SDepartment();
            if (string.IsNullOrEmpty(rootDepartmentId))
            {
                return 101142;
            }
            if (ReadDepartment(rootDepartmentId) == null)
            {
                return 101143;
            }
            modelOpt = dataOpt.GetModel(rootDepartmentId);
            if (modelOpt.ifLeaf != 1)
            {
                return 101144;
            }
            try
            {
                try
                {
                    ORM.DAL.SURD optSURD = new ORM.DAL.SURD();
                    DataSet myDs = optSURD.GetList("departID = " + rootDepartmentId);
                    decimal[] delId = new decimal[] {};
                    for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
                    {
                        delId[i] = Convert.ToDecimal(myDs.Tables[0].Rows[i]["id"]);
                    }
                    foreach (int j in delId)
                    {
                        optSURD.Delete(delId[j]);
                    }

                }
                catch (Exception)
                {
                    Operator.MakeLog(ObjectType.DEPT, "101145 " + rootDepartmentId);                    
                    return 101145;
                }
                bool OK = dataOpt.Delete(rootDepartmentId);
                if (OK)
                {
                    Operator.MakeLog(ObjectType.DEPT, "101141 " + rootDepartmentId);
                    return 101141;
                }
                else
                {
                    throw new Exception("SDepartment dataOpt.Delete()失败");
                }
            }
            catch (Exception)
            {
                Operator.MakeLog(ObjectType.DEPT, "101149 " + rootDepartmentId);
                return 101149;
            }
        }

        /// <summary>
        /// 删除根栏目
        /// </summary>
        /// <param name="rootDepartmentObject">根栏目对象</param>
        public static int DeleteRootDepartment(Department rootDepartmentObject)
        {
            //*****10115*****
            //调用DeleteRootDepartment方法
            //--------------------------------------------------------
            return DeleteRootDepartment(rootDepartmentObject.Id);
        }
        #endregion StaticFunctions
    }
}
